package h0715;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
  * @description 火星文计算. 使用栈
  * @author 不知名帅哥
  * @date 2024/7/15 19:32
  * @version 1.0
*/
public class LeetspeakCompute {
    // 返回操作符的优先级
    private static int precedence(String ch) {
        switch (ch) {
            case "#":
                return 1;
            case "$":
                return 2;
        }
        return -1;
    }

    // 将中缀表达式转换为后缀表达式
    private static List<String> infixToPostfix(List<String> exp) {
        List<String> result = new ArrayList<>();
        Stack<String> stack = new Stack<>();

        for (int i = 0; i < exp.size(); i++) {
            String s = exp.get(i);

            // 如果是操作数，直接添加到结果中
            if (s.matches("\\d+")) {
                result.add(s);
            }
            // 当前字符是操作符
            else {
                while (!stack.isEmpty() && precedence(s) <= precedence(stack.peek())) {
                    result.add(stack.pop());
                }
                stack.push(s);
            }
        }
        // 弹出所有剩余的操作符
        while (!stack.isEmpty()) {
            result.add(stack.pop());
        }
        return result;
    }

    //计算后缀表达式的值
    private static int evaluatePostfix(List<String> exp) {
        Stack<Integer> stack = new Stack<>();


        for (int i = 0; i < exp.size(); i++) {
            String c = exp.get(i);
            if (c.matches("\\d+")) {
                stack.push(Integer.parseInt(c));
            } else {
                Integer val1 = stack.pop();
                Integer val2 = stack.pop();
                switch (c) {
                    case "$":
                        int temp = val2 * 3 + val1 + 2;
                        stack.push(temp);
                        break;
                    case "#":
                        int temp2 = 2 * val2 + 3 * val1 + 4;
                        stack.push(temp2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();

        Pattern compile = Pattern.compile("[#\\$]|(\\d+)");
        Matcher matcher = compile.matcher(s);
        List<String> tokens=new ArrayList<>();
        while (matcher.find()) {
            tokens.add(matcher.group());
        }

        //	7#6$5#12
        //中缀转后缀
        //计算后缀表达式
        List<String> s1 = infixToPostfix(tokens);
        System.out.println(evaluatePostfix(s1));
    }
}
